#include "widget.h"

#include <QPushButton>
#include <QMessageBox>
#include <QFontDialog>
#include <QColorDialog>
#include <QFileDialog>
#include <QColor>
#include <QDebug>
#include <QFont>
#include <QPlainTextEdit>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    resize(1000, 800);

    QPushButton *messBtn = new QPushButton("测试消息对话框", this);
    messBtn->resize(300, 50);
    messBtn->move(100, 100);

    connect(messBtn, &QPushButton::clicked, [=]{
        QMessageBox::critical(this, "严重错误", "有危险，快跑");
        //QMessageBox::information(this, "提示", "注意弟弟背刺");
        //QMessageBox::question(this, "问题", "出现问题了");
        //QMessageBox::warning(this, "警告", "警告信息");
    });

    QPushButton *colorBtn = new QPushButton("测试颜色对话框", this);
    colorBtn->resize(300, 50);
    colorBtn->move(100, 200);

    connect(colorBtn, &QPushButton::clicked, [=]{
        QColor col = QColorDialog::getColor();
        qDebug() << "r = " << col.red()
                 << "g = " << col.green()
                 << "b = " << col.blue();
    });

    QPushButton *fontBtn = new QPushButton("测试字体对话框", this);
    fontBtn->resize(300, 50);
    fontBtn->move(100, 300);
    connect(fontBtn, &QPushButton::clicked, [=]{
        bool ok;
        QFont font = QFontDialog::getFont(&ok);
        qDebug() << "字体: " << font.family()
                 << "字号: " << font.pointSize()
                 << "字体风格: " << font.styleName();
    });

    QPushButton *fileBtn = new QPushButton("测试文件对话框", this);
    fileBtn->resize(300, 50);
    fileBtn->move(100, 400);

    QPlainTextEdit *pte = new QPlainTextEdit(this);
    pte->move(450, 50);
    pte->resize(500, 700);

    connect(fileBtn, &QPushButton::clicked, [=]{
       QString path =
               QFileDialog::getOpenFileName(this, "xxx"
                                            ,"C:/Users/lili/Desktop/嵌入式3期"
                                            , "(*.xmind);;(*.txt)");
       qDebug() << "path = " << path;

       QFile fle(path);//打开文件
       fle.open(QIODevice::ReadOnly);//指定打开模式

       QByteArray ba = fle.readAll();//将文件中的数据一次读完

       QString msg(ba);//将QByteArray数据转换为QString

       pte->setPlainText(msg);

       fle.close();//关闭文件
    });
}

Widget::~Widget()
{
}

